package me.lc.linklink.util;

import me.lc.linklink.core.GameArrangeType;
import me.lc.linklink.core.GameSystem;
import me.lc.linklink.entity.Location;
import me.lc.linklink.sprite.LLKBlock;

public class NodeMoveHelper {
	private static LLKBlock[][] blocks;

	public static void moveNode(LLKBlock[][] blocks, int i, int j,
			GameArrangeType arrangeType) {
		NodeMoveHelper.blocks = blocks;
		switch (arrangeType) {
		case Left:
			MoveLeft(i);
			break;
		case Right:
			MoveRight(i);
			break;
		case Bottom:
			MoveBottom(j);
			break;
		case Top:
			MoveTop(j);
			break;
		case TopBottom:
			MoveTopBottom(j);
			break;
		case LeftRight:
			MoveLeftRight(i);
			break;
		default:
			break;
		}
	}

	private static void MoveLeft(int row) {
		// 找到一个空的，往后寻找，找到替换，没找到break
		for (int j = 1; j < GameSystem.GameColumns + 1; j++) {
			LLKBlock block = blocks[row][j];
			if (block.getBlockItemIndex() < 0) {
				LLKBlock find = null;
				int findJ = 0;
				for (int jj = j + 1; jj < GameSystem.GameColumns + 1; jj++) {
					LLKBlock temp = blocks[row][jj];
					if (temp.getBlockItemIndex() >= 0) {
						find = temp;
						findJ = jj;
						break;
					}
				}
				if (find != null) {
					// 互换并更新界面
					exchangeBlock(row, j, row, findJ);
				} else {
					break;
				}
			}
		}
	}

	private static void MoveRight(int row) {
		// 找到一个空的，往后寻找，找到替换，没找到break
		for (int j = GameSystem.GameColumns; j > 0; j--) {
			LLKBlock block = blocks[row][j];
			if (block.getBlockItemIndex() < 0) {
				LLKBlock find = null;
				int findJ = 0;
				for (int jj = j - 1; jj > 0; jj--) {
					LLKBlock temp = blocks[row][jj];
					if (temp.getBlockItemIndex() >= 0) {
						find = temp;
						findJ = jj;
						break;
					}
				}
				if (find != null) {
					// 互换并更新界面
					exchangeBlock(row, j, row, findJ);
				} else {
					break;
				}
			}
		}
	}

	private static void MoveBottom(int column) {
		// 找到一个空的，往后寻找，找到替换，没找到break
		for (int i = 1; i < GameSystem.GameRows + 1; i++) {
			LLKBlock block = blocks[i][column];
			if (block.getBlockItemIndex() < 0) {
				LLKBlock find = null;
				int findI = 0;
				for (int ii = i + 1; ii < GameSystem.GameRows + 1; ii++) {
					LLKBlock temp = blocks[ii][column];
					if (temp.getBlockItemIndex() >= 0) {
						find = temp;
						findI = ii;
						break;
					}
				}
				if (find != null) {
					// 互换并更新界面
					exchangeBlock(i, column, findI, column);
				} else {
					break;
				}
			}
		}
	}

	private static void MoveTop(int column) {
		for (int i = GameSystem.GameRows; i > 0; i--) {
			LLKBlock block = blocks[i][column];
			if (block.getBlockItemIndex() < 0) {
				LLKBlock find = null;
				int findI = 0;
				for (int ii = i - 1; ii > 0; ii--) {
					LLKBlock temp = blocks[ii][column];
					if (temp.getBlockItemIndex() >= 0) {
						find = temp;
						findI = ii;
						break;
					}
				}
				if (find != null) {
					// 互换并更新界面
					exchangeBlock(i, column, findI, column);
				} else {
					break;
				}
			}
		}
	}

	private static void MoveTopBottom(int column) {
		// Top Part
		for (int i = GameSystem.GameRows; i > GameSystem.GameRows / 2; i--) {
			LLKBlock block = blocks[i][column];
			if (block.getBlockItemIndex() < 0) {
				LLKBlock find = null;
				int findI = 0;
				for (int ii = i - 1; ii > GameSystem.GameRows / 2; ii--) {
					LLKBlock temp = blocks[ii][column];
					if (temp.getBlockItemIndex() >= 0) {
						find = temp;
						findI = ii;
						break;
					}
				}
				if (find != null) {
					// 互换并更新界面
					exchangeBlock(i, column, findI, column);
				} else {
					break;
				}
			}
		}

		// Bottom Part
		for (int i = 1; i < (GameSystem.GameRows / 2 + 1); i++) {
			LLKBlock block = blocks[i][column];
			if (block.getBlockItemIndex() < 0) {
				LLKBlock find = null;
				int findI = 0;
				for (int ii = i + 1; ii < (GameSystem.GameRows / 2 + 1); ii++) {
					LLKBlock temp = blocks[ii][column];
					if (temp.getBlockItemIndex() >= 0) {
						find = temp;
						findI = ii;
						break;
					}
				}
				if (find != null) {
					// 互换并更新界面
					exchangeBlock(i, column, findI, column);
				} else {
					break;
				}
			}
		}
	}

	private static void MoveLeftRight(int row) {
		// left part
		for (int j = 1; j < (GameSystem.GameColumns / 2 + 1); j++) {
			LLKBlock block = blocks[row][j];
			if (block.getBlockItemIndex() < 0) {
				LLKBlock find = null;
				int findJ = 0;
				for (int jj = j + 1; jj < (GameSystem.GameColumns / 2 + 1); jj++) {
					LLKBlock temp = blocks[row][jj];
					if (temp.getBlockItemIndex() >= 0) {
						find = temp;
						findJ = jj;
						break;
					}
				}
				if (find != null) {
					// 互换并更新界面
					exchangeBlock(row, j, row, findJ);
				} else {
					break;
				}
			}
		}

		// right part
		for (int j = GameSystem.GameColumns; j > GameSystem.GameColumns / 2; j--) {
			LLKBlock block = blocks[row][j];
			if (block.getBlockItemIndex() < 0) {
				LLKBlock find = null;
				int findJ = 0;
				for (int jj = j - 1; jj > GameSystem.GameColumns / 2; jj--) {
					LLKBlock temp = blocks[row][jj];
					if (temp.getBlockItemIndex() >= 0) {
						find = temp;
						findJ = jj;
						break;
					}
				}
				if (find != null) {
					// 互换并更新界面
					exchangeBlock(row, j, row, findJ);
				} else {
					break;
				}
			}
		}
	}

	private static void exchangeBlock(int i1, int j1, int i2, int j2) {
		LLKBlock temp = blocks[i1][j1];
		blocks[i1][j1] = blocks[i2][j2];
		blocks[i2][j2] = temp;

		// 更新Location
		blocks[i1][j1].setLocation(new Location(i1 - 1, j1 - 1));
		blocks[i2][j2].setLocation(new Location(i2 - 1, j2 - 1));

	}
}
